#add descriptive statistics, this version has split between compliers, indirect compliers, and NTs

percent <- function(x,digits = 0) paste0(round(x,(digits + 2))*100,"%")

#calculate percentage of midline respondents who are parents
ml%>%
  group_by(parent)%>%
  summarize(n=n())%>%
  mutate(freq = n / sum(n))

# Create the simple version of compliance variable ------------------

ml$compliance_binary <- ml$compliance

ml$compliance_binary[ml$compliance_label %in% c("Never-Taker", "Apprised Never-Taker", "Indirect Complier")] <- 0
ml$compliance_binary[ml$compliance_label == "Complier"] <- 1


#create tables
desc_tab <- 
  ml %>% 
  group_by(compliance_binary, female) %>% 
  summarise(
    N = length(age),
    parent = mean(parent, na.rm = T) %>% percent,
    tv = mean(tv) %>% percent,
    age = mean(age) %>% round(0),
    radio = mean(radio) %>% percent,
    rooms = mean(rooms) %>% round(0),
    highest_grade = mean(highest_grade) %>% round(1),
    illiterate = mean(illiterate) %>% percent,
    travel_big_city = mean(travel_big_city) %>% percent
  ) %>% 
  arrange(compliance_binary,female) %>% 
  ungroup() %>% 
  group_by(compliance_binary) %>% 
  mutate(proportion = (N / sum(N)) %>% percent,
         N_prop = paste0(N, " (",proportion,")"),
         label = case_when(
           compliance_binary == 0 ~ "Never-Takers",
           compliance_binary == 1 ~ "Compliers"),
         gender = case_when(
           female == 1 ~ "Women",
           female == 0 ~ "Men")) %>% 
  ungroup() %>% 
  dplyr::select(label, gender, N_prop, parent, age, tv, radio,
         rooms,highest_grade, illiterate, travel_big_city) %>% 
  rename("Stratum" = label, 
         "Gender" = gender,
         "N" = N_prop,
         "Parent" = parent,
         "Age" = age,
         "Own TV" = tv,
         "Own Radio" = radio,
         "Rooms in house" = rooms,
         "Highest grade [0,16]" = highest_grade,
         "Illiteracy" = illiterate,
         "Has visited big city" = travel_big_city
  )

desc_tab_body <- desc_tab[,3:ncol(desc_tab)] %>% t()
desc_tab_head <- desc_tab[,1:2] %>% t()
desc_tab_head <- rbind(
	Stratum = c("Never-Takers","","Compliers",""),
	Gender = c("Men","Women","Men","Women")
  
)
desc_tab_full <- rbind(desc_tab_head, desc_tab_body)

sink("03_tables/stratum_description.tex")
desc_tab_full %>% 
  xtable(align = c("r|cc|cc")) %>% 
  print.xtable(floating = FALSE,
               include.colnames = FALSE,
               hline.after = c(1,3))
sink()



